 <?php
	 session_start();	//start session
	 if(!isset($_SESSION['username'])){	//check if the user data is in the session
		 header("location: Login.php");	//if not redirect back to user
	 }
 ?>

<html>
	<head>
		<title></title>
		<link rel="stylesheet" href="style.css">
		<script src="jquery-1.11.0.js"></script>
		<script src="gen.js"></script>
		
	<script>
	
		function syncAjax(u){
			var obj=$.ajax(	{url:u, async:false} );
			return $.parseJSON(obj.responseText);
		}

			function displayMessage(message){
				$('#divStatus').text(message);
			}

		function community_delete(obj,id){
			var cid = document.getElementById("community_id").value;
			var u = "action.php?cmd=2&id=" + id;
			var a;
			var b = confirm("Are you sure you want to delete");
			if (!b){
				return;
			}
			else{
						console.log(u);

			syncAjax(u);
			}
			deletePage();
		}	

		function deletePage(){
			var u = "action.php?cmd=3";
			$(".row1").remove();
			$(".row2").remove();
			//.row1().remove();
			
			var a = document.getElementById("myTable");
			var shamir = syncAjax(u);
			for(var i = 0; i < shamir.community.length; i++){
				console.log("fjkdjfkd");
				var phy = a.insertRow(i+1);
				var cell1 = phy.insertCell(0);
				var cell2 = phy.insertCell(1);
				var cell3 = phy.insertCell(2);
				var cell4 = phy.insertCell(3);

				if(i%2==0)
					phy.className="row1";
				else
					phy.className="row2";

				cell1.innerHTML = shamir.community[i].community_id;
				cell2.innerHTML = "<a href = community_detail.php?community_id=" + shamir.community[i].community_id + ">" + shamir.community[i].community_name +"</a>";
				cell3.innerHTML = "<a href = communityedit.php?community_id=" + shamir.community[i].community_id + "> edit </a>";
				cell4.innerHTML = "<span class='hotspot' onclick='community_delete(this," + shamir.community[i].community_id + ")'>delete<span>";

			}

		}

			// function myPopup(){
			// 	var x;
			// 	var community_name = prompt("community name", "abc");
			// 	var population = prompt("population","12");
			// 	var household = prompt("household","24");
			// }

			function edit(obj,id){

				var r = getCommunity(id);

				if(r.result==0){
					$("#divStatus").text(r.message);
					return;
				}
				
				$("#ecommunity_name").prop("value", r.community_name);
				$("#epopulation").prop("value", r.population);
				$("#ehousehold").prop("value", r.household);
				
				var y = event.clientY;
				
				var x = event.clientX/2;
				
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				
				$("#divEdit").fadeIn(1000);
				//create global varial to take in idparameter

			}
			
			function getCommunity(id){
				var u = "action.php?cmd=1&id=" + id;
				return syncAjax(u);
			}

			function cancels(){
			//	$("#divAdd").css("background-color","#ffffff");
				$("#divAdd").fadeOut(500);
			}

			function cancel(){
				//fade out the form in half a second
					//$("#divEdit").css("background-color","#ffffff");
					$("#divEdit").fadeOut(500);
				}

			function save(){
				//complete the url

				var id = document.getElementById("community_id").value;
				//var id = 5;	
				//var id = $_REQUEST['id'];
				var cn = document.getElementById("ecommunity_name").value;
				var p = document.getElementById("epopulation").value;
				var h = document.getElementById("ehousehold").value;

				var u = "action.php?cmd=4&id=" + id + "&community_name=" + cn + "&population=" + p + "&household=" + h; //change the cmd to 3 to update the records
				
				var a= syncAjax(u);
				//$.getJSON(u,saveDone);
				// if(r.result == 1){
				// 	alert("saved");
				// }else{
				// 	alert("not saved");
				

				// $("#divEdit").css("background-color","#ffffff");
				// $("#divEdit").fadeOut(500);

				cancel();
				//location.reload();

				deletePage();
				displayMessage("Your record has been edited");

			}

			function add(){
				// 	// var r = something;

				// //if(r.result==0){
				// //	$("#divStatus").text(r.message);
				// //	return;
				// //}
				
				// $("#community_name").prop("value", r.community_name);
				// $("#longitude").prop("value", r.longitude);
				// $("#latitude").prop("value", r.latutude);
				// $("#population").prop("value", r.population);
				// $("#household").prop("value", r.household);


				$("#community_name").prop("value", "community_name");
				$("#longitude").prop("value","logitude" );
				$("#latitude").prop("value", "latutude");
				$("#population").prop("value", "population");
				$("#household").prop("value"," household");
				$("#subdistrict_id").prop("value","subdistrict_id");
				
				var y = event.clientY;
				
				var x = event.clientX/2;
				
				$("#divAdd").css("top",y);
				$("#divAdd").css("left",x);
				
				$("#divAdd").fadeIn(1000);
			}

			function addSave(){
				//complete the url

				//var id = document.getElementById("community_id");
				//var id = 5;	
				//var id = $_REQUEST['id'];
				var cn = document.getElementById("community_name").value;
				var lng = document.getElementById("longitude").value;
				var lat = document.getElementById("latitude").value;
				var p = document.getElementById("population").value;
				var h = document.getElementById("household").value;
				var sid = document.getElementById("subdistrict_id").value;

				var u = "action.php?cmd=5&community_name=" + cn + "&longitude=" + lng + "&latitude=" + lat + "&population=" + p + "&household=" + h + "&subdistrict_id=" + sid; //change the cmd to 3 to update the records
				
				var a= syncAjax(u);
				//$.getJSON(u,saveDone);

				cancels();
				//location.reload();

				deletePage();

				displayMessage("Your record has been added");

			}


			function saveDone(data){
				if(data == 1){
					alert("saved");
				}else{
					alert("not saved");
				}

			}

		
	</script>
</head>

	<body>
		<table  id="contentTable">
			<tr>
				<td colspan="2" id="pageheader">
					HEALTH INFORMATION SYSTEMS
				</td>
			</tr>
			<tr>
				<td id="mainnav">
					<div class="menuitem"><a href="Home.php">location</a></div>
					<div class="menuitem">opd cases</div>
					<div class="menuitem">health promotion</div>
					<div class="menuitem">nutrition</div>
					<div class="menuitem">child welfare</div>
					<div class="menuitem">family planning</div>
					<div class="menuitem"><a href="userslist.php">users</a></div>
					<div class="menuitem"><a href="logout.php">logout<a></div>
				</td>
				<td id="content">
					<div id="divPageMenu">
						<span class="menuitem" >Community Health Officer</span> | 
						<span class="menuitem" >Districts</span> | 
						<span class="menuitem" ><a href="subdistrict_list.php">Sub districts</a></span> | 
						<span class="menuitem" ><a href="community_lists.php">Communities<a/></span>
					</br>
						<form action="community_lists.php" method="GET">
						<input type="text" name="community_name" />
						<span class="menuitem"> <input type="submit" name="submit" value="Search" /></span>
						</form>
					</div>
					<div id="divStatus" class="status">
						
					</div>

					<div id="divContent">
					<div id = 'addNew'><span class='hotspot' onclick='add()'>Add a community<span></div>
						<table class="reportTable" width="100%" id = "myTable">
							<tr class="header" >
								<td>ID</td>
								<td>COMMUNITY NAME</td>
								<td></td>
								<td></td>
							</tr>	
							
		<?php	
		
			
			$community_name = "";
			if(isset($_REQUEST["community_name"])){
				$community_name = $_REQUEST["community_name"];
			}
		
		include("community.php");
		$obj = new community();
		if(!$obj -> search_community($community_name)){
		echo"error";
		exit();
		}

		   // if(!$obj -> search_community($community_name)){
			//	echo mysql_error($obj->link);
			//	exit();
			//}
			$row = $obj->fetch();
	
			if(is_null($row['community_name'])){
				echo"<p>can not find a community with that name</p>";
			}
			else{


	
	//$row = $obj->fetch();
	$row_counter = 0;
	while($row){
	
			
		if($row_counter%2==0){
			$style=" class='row1' ";
		}else{
			$style=" class='row2'  ";
		}
		
		$id = $row['community_id'];
		?>
		<input type = "hidden" value = <?php echo "$id"; ?> id ="community_id">
	
		<?php
		echo "<tr $style >";
		echo "<td>";
			echo $row["community_id"];
		echo "</td>";
		
		echo "<td>";
			echo "<a href = 'community_detail.php?community_id=$id'>" . $row["community_name"] . "</a>";
		echo "</td>";
		
		//echo ("<td>");
				echo "<td><span class='hotspot' onclick='edit(this,$id)'>edit<span></td>";

				//echo ("<a target='_blank' href='communityedit.php?community_id=$id'>edit</a>");
				//echo ("</td>");
				
				echo "<td><span class='hotspot' onclick='community_delete(this,$id)'>delete<span></td>";
				echo "</tr>";
		$row=$obj->fetch();
		$row_counter++;
	}
	
	
	//mysql_close($link);
	
	}
?>
		
						</table>
						
					</div>

			</tr>
			</table>


	<div id = "divAdd" class = "popupForm">
		<table class= "tableForm" >
					<tr>
						<td class="label">Community Name: </td>
						<td class="field"><input type="text" value="" id="community_name" ></td>
					</tr>
					<tr>
						<td class="label">Longitude: </td>
						<td class="field"><input type="text" value="" id="longitude" ></td>
					</tr>
					<tr>
						<td class="label">Latitude: </td>
						<td class="field"><input type="text" value="" id="latitude" ></td>
					</tr>

					<tr>
						<td class="label">Population:</td> 
						<td class="field"><input type="text" value="" id="population" >
						</td>
					</tr>
					<tr>
						<td class="label">Household:</td>
						<td class="field"><input type="text" value="" id="household" >
						</td>
					</tr>
					<tr>
						<td class = "label"> Subdistrict: </td>
						<td><select id="subdistrict_id">
							<option value = "0" > All	</option>
							<option value = "1" > Aburi </option>
							<option value = "2"> Pokrum </option></td>	
						</select>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<input type="button" value="save" onclick="addSave()" >
							<input type="button" value="cancel" onclick="cancels()" >
						</td>
					</tr>
		</table>

	</div>
	<div id="divEdit" class="popupForm">
		<table class="tableForm" >
					<tr>
						<td class="label">Community Name: </td>
						<td class="field"><input type="text" value="<?php echo $row['community_name'] ?>" id="ecommunity_name" ></td>
					</tr>
					<tr>
						<td class="label">Population:</td> 
						<td class="field"><input type="text" value="" id="epopulation" >
						</td>
					</tr>
					<tr>
						<td class="label">Household:</td>
						<td class="field"><input type="text" value="" id="ehousehold" >
						</td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<input type="button" value="save" onclick="save()" >
							<input type="button" value="cancel" onclick="cancel()" >
						</td>
					</tr>
			</table>
				
	</div>		
	
	</body>
</html>	